# -*- coding: utf-8 -*-
########################################################################
#
# (c) 2010-2011 Markus Dittrich
#
# This program is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public
# License Version 3 as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License Version 3 for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the Free
# Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
#
#######################################################################

from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from __future__ import absolute_import

from PyQt4.QtCore import (QString, QStringList, Qt, QTime) 
from PyQt4.QtGui import (QTableWidget, QTableWidgetItem, QBrush)


DAY_LIST = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
TIME_START = 8
TIME_END = 20
TIME_INTERVAL = 0.5


class EventWidget(QTableWidget):
    """ This class defines the event widget """

    def __init__(self, parent = None):

        super(EventWidget, self).__init__(parent)



    def update(self, currentDate, dataBase):
        """ This function updates the event widget with the
        events of the given week. """

        # initialize the widget
        self.setColumnCount(len(DAY_LIST))
        self.setRowCount((TIME_END - TIME_START + 1)/ TIME_INTERVAL)
        for row in range(0, self.rowCount()):
            for col in range(0, self.columnCount()):
                self.setItem(row, col, EventWidgetItem())

        # set up time labels
        timeLabels = QStringList()
        for hour in range(TIME_START, TIME_END + 1):
            time1 = QTime(hour, 0)
            time2 = QTime(hour, 30)
            timeLabels << time1.toString("h:mm") << time2.toString("h:mm")
        self.setVerticalHeaderLabels(timeLabels)

        # add date labels and add daily events if any
        currentDayOfWeek = currentDate.dayOfWeek()
        headers = QStringList()
        for (index, day) in enumerate(DAY_LIST):
            dayDate = currentDate.addDays(index - currentDayOfWeek + 1)
            headers << day + "  " + str(dayDate.day())
            
            events = dataBase.get_events_by_date(dayDate.toPyDate())
            for event in events:
                hour = QTime.fromString(event["time"]).hour()
                minutes = QTime.fromString(event["time"]).minute()
                if minutes < 30:
                    rowAdd = 0
                else:
                    rowAdd = 1
                    
                print(event, hour, minutes)
                row = (hour - 8)/TIME_INTERVAL + rowAdd

                item = EventWidgetItem(event)
                self.setItem(row, index, item)
                self.setSpan(row, index, 2, 1)

        self.setHorizontalHeaderLabels(headers)
    



class EventWidgetItem(QTableWidgetItem):
    """ This class describes a single entry in the
    TableWidgetItem. 

    """


    def __init__(self, event = None, parent = None):

        super(EventWidgetItem, self).__init__("")

        self._event = event
        if self._event:
            self.setText(event["title"])
            self.setBackground(QBrush(Qt.red))
            self.setToolTip(event["description"])
            
